﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
//using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BootyBay.StoreAppMgmtCore;

namespace BootyBay.WPFAgent
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            HandleSystemTrayIcon();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Task.Run(() => StartAgentService());
        }

        async Task StartAgentService()
        {


            string userName = UserProfileRepository.GetCurrentUserDisplayName();

            PrintMsg("Logging for the current user: \"" + userName + "\"");

            StoreAppInitializer appInit = new StoreAppInitializer();
            appInit.OnNotificationOutput += session_OnNotificationOutput;
            appInit.StartInstallStoreApp();

            AgentServiceSession session = new AgentServiceSession(userName);
            session.OnNotificationOutput += session_OnNotificationOutput;

            PrintMsg("Agent session starting...");
            session.Initialize();

            session.StartMonitoringAppMgmtTaskAsync();
            session.StartMonitoringServerRequestedAppMgmtTaskAsync();

        }

        void session_OnNotificationOutput(object sender, AppMgmtEventArgs args)
        {

            PrintMsg(args.EventMessage);


        }

        void PrintMsg(string msg)
        {
            Dispatcher.BeginInvoke((Action)delegate()
                 {
                     tbLog.Text += DateTime.Now.ToString() + " - " + msg + System.Environment.NewLine;

                     if (!tbLog.IsMouseOver)
                     {
                         tbLog.ScrollToEnd();
                     }

                 });
        }


        private void HandleSystemTrayIcon()
        {
            System.Windows.Forms.ContextMenuStrip contextMenu = new System.Windows.Forms.ContextMenuStrip();

            System.Windows.Forms.ToolStripMenuItem menuItemLaunchStoreApp = new System.Windows.Forms.ToolStripMenuItem("Launch Store App");
            menuItemLaunchStoreApp.Click += (o1, e1) =>
            {
                Uri storeAppProtocol = new Uri("bootybay:");
                Process.Start(new ProcessStartInfo(storeAppProtocol.ToString()));
            };

            System.Windows.Forms.ToolStripMenuItem menuItemExit = new System.Windows.Forms.ToolStripMenuItem("Exit");
            menuItemExit.Click += (o1, e1) =>
            {
                Application.Current.Shutdown();

            };

            contextMenu.Items.Add(menuItemLaunchStoreApp);
            contextMenu.Items.Add(menuItemExit);

            System.Drawing.Icon systemTrayIcon;
            using (var iconStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("BootyBay.WPFAgent.Assets.favicon.ico"))
            {
                systemTrayIcon = new System.Drawing.Icon(iconStream);
            }


            MinimizeToTray.Enable(this, contextMenu, systemTrayIcon);

            this.Closing += MainWindow_Closing;



        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void tbHomepage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Uri codeplexUri = new Uri("http://bootybay.codeplex.com");
            Process.Start(new ProcessStartInfo(codeplexUri.ToString()));
        }

        private void tbContactUs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Uri mailtoUri = new Uri("mailto:bootybay@microsoft.com?subject=About the private store");
            Process.Start(new ProcessStartInfo(mailtoUri.ToString()));
        }

        private void tbCopyAllLog_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            string allLogs = tbLog.Text;
            Clipboard.SetText(allLogs);
        }




    }
}
